package com.yh.zz_hlj.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;

import com.yh.zz_hlj.AppContext;
import com.yh.zz_hlj.R;
import com.yh.zz_hlj.api.RetrofitSingleton;
import com.yh.zz_hlj.base.BaseFragment;
import com.yh.zz_hlj.common.PLog;
import com.yh.zz_hlj.common.StringUtils;
import com.yh.zz_hlj.common.TDevice;
import com.yh.zz_hlj.modules.bean.Result;
import com.yh.zz_hlj.modules.ui.LoginActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static android.content.Context.VIBRATOR_SERVICE;


/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：ChangePwdFragment
 * <p>
 * 创建时间：2017.06.05.15:25
 * *****************************
 */
public class ChangePwdFragment extends BaseFragment {

    @InjectView(R.id.et_old_pwd)
    EditText etOldPwd;
    @InjectView(R.id.til_old_pwd)
    TextInputLayout tilOldPwd;
    @InjectView(R.id.et_new_pwd)
    EditText etNewPwd;
    @InjectView(R.id.til_new_pwd)
    TextInputLayout tilNewPwd;
    @InjectView(R.id.et_sure_pwd)
    EditText etSurePwd;
    @InjectView(R.id.til_pwd)
    TextInputLayout tilPwd;
    @InjectView(R.id.btn)
    Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.fragment_change_pwd, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        TDevice.hideSoftKeyboard(mActivity);
        btn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn:
                String oldPwd = etOldPwd.getText().toString().trim();
                String pwd = AppContext.getInstance().getProperty("user.pwd");
                String userName = AppContext.getInstance().getProperty("user.account");
                String newPwd = etNewPwd.getText().toString().trim();
                String surePwd = etSurePwd.getText().toString().trim();
                if (pwd.equals(oldPwd)) {
                    tilOldPwd.setError(null);
                    tilOldPwd.setErrorEnabled(false);
                    if (newPwd.equals(surePwd)) {
                        if (StringUtils.isEmpty(newPwd)) {
                            tilNewPwd.setError("请输入新密码");
                            getFouce(etNewPwd);
                            getVibrate(etNewPwd);
                        } else {
                            tilNewPwd.setError(null);
                            tilNewPwd.setErrorEnabled(false);

                            tilPwd.setError(null);
                            tilPwd.setErrorEnabled(false);
                            showWaitDialog();
                            RetrofitSingleton.getApiService(mActivity).
                                    EditPassWord(userName, oldPwd, newPwd)
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(observer);
                        }

                    } else {
                        tilPwd.setError("密码前后不一致");
                        tilNewPwd.setError(null);
                        tilNewPwd.setErrorEnabled(false);
                        getFouce(etSurePwd);
                        getVibrate(etSurePwd);
                    }
                } else {
                    getFouce(etOldPwd);
                    getVibrate(etOldPwd);
                    tilOldPwd.setError("老密码输入错误");
                }
                break;
        }
    }


    //震动
    private void getVibrate(EditText et) {
        Animation shake = AnimationUtils.loadAnimation(mActivity, R.anim.shake);
        et.startAnimation(shake);
    }

    //重新获得焦点
    private void getFouce(EditText et) {
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        et.findFocus();
        Vibrator vibrator = (Vibrator) mActivity.getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(1000);//震动1s
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    protected Observer<Result> observer = new Observer<Result>() {
        @Override
        public void onCompleted() {
            PLog.d("s", "onCompleted");
            hideWaitDialog();
        }

        @Override
        public void onError(Throwable e) {
            PLog.d("s", e.getMessage());
            Snackbar.make(mActivity.getWindow().getDecorView(), e.getMessage(), Snackbar.LENGTH_SHORT).show();
            hideWaitDialog();
        }

        @Override
        public void onNext(Result result) {
            hideWaitDialog();
            if (result != null) {
                if (result.isSuccess()) {
                    AppContext.getInstance().logout();
//                    AppContext.getInstance().Logout();
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    mActivity.startActivity(intent);
                } else {

                }
            }
        }
    };
}
